/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var largestValues = function(root) {
  let ret = [];
  let traverse = (node, depth) => {
      ret[depth] = ret[depth] === undefined ? node.val : Math.max(ret[depth], node.val);
      if(node.left) traverse(node.left, depth + 1);
      if(node.right) traverse(node.right, depth + 1);
  }
  root && traverse(root, 0);
  return ret;
};